// 预加载
$(document).ready(function () {
    layui.use(['form', 'layedit', 'laydate'], function(){
        var form = layui.form
            ,layer = layui.layer
            ,layedit = layui.layedit
            ,laydate = layui.laydate;

        //日期
        laydate.render({
            elem: '#date'
            ,format: 'yyyyMMdd'
        });
        // 监听提交
        form.on('submit(demo1)', function(data){
            // 监听按钮提交表单信息
            addOrder($('#user_id').val());
            return false;
        });

        // 搜索
        form.on('submit(formDemo)', function (data) {
            if (data.field.keyword == '') {
                layer.msg('请先输入搜索词');
                return false;
            } else {
                // 请求数据
                $.ajax({
                    type:'get',
                    url: "/order/tmlData?idCard=" + data.field.keyword,
                    success: function(res) {
                        console.log(res);
                        onData(res);
                    }
                })
                return false;
            }
        });
    })
});
// 渲染数据模板
function onData(res) {
    if (res.status == 1) {
        // 没有该用户数据
        layui.use(['laytpl'], function () {
            var laytpl = layui.laytpl

            var myDatas = {
                //数据
                "dataInfo": res.msg,
                "status": 1
            };
            var getTpl = addData.innerHTML, view = $("#addInfo");
            laytpl(getTpl).render(myDatas, function (result) {
                //清空元素内部的html代码
                view.empty();
                //重新添加
                view.append(result);
            });
        })
    } else {
        layui.use(['laytpl','form','laydate'], function () {
            var laytpl = layui.laytpl
                ,form = layui.form
                ,laydate = layui.laydate;

            var myDatas = {
                //数据
                "dictList": res.data.dictList,
                "setmealList": res.data.setmealList,
                "userInfo": res.data.userInfo,
                // "dataInfo": res.data,
                "status": 0
            };
            var getTpl = addData.innerHTML, view = $("#addInfo");
            laytpl(getTpl).render(myDatas, function (result) {
                //清空元素内部的html代码
                view.empty();
                //重新添加
                view.append(result);
                laydate.render({
                    elem: '#date'
                    ,format: 'yyyyMMdd'
                });
                form.render('select');
            });
        })
    }
};

// 添加订单
function addOrder(user_id) {
    $.ajax({
        type: "post",
        url: "/order/addOrderInfo",
        dataType: "json",
        traditional: true,
        contentType : 'application/json;charset=utf-8',
        data:
            JSON.stringify({
                user_id: user_id,
                setmealId: $('#setmeal').val(),
                ordertotal: $('#orderTotal').val(),
                orderpaid: $('#orderPaid').val(),
                orderadvance: $('#orderAdvance').val(),
                bm001: $('#bm001').val(),
                validtime: $('#date').val().toString(),
            }),
        success: function(res) {
            console.log("添加成功");
            layui.use('layer', function(){
                var layer = layui.layer;
                layer.msg('添加成功');
                var data = {
                    status: 1,
                    msg: res.msg
                }
                onData(data);
                $('#keyword').val('')
            })
        }
    })
}